﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.FormatOutput;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOrderShipmentMessage : FormTemplet
    {
        private OrderCondition _orderCondition = null;

        private DataTable _dt = null;

        public FormOrderShipmentMessage()
        {
            this.InitializeComponent();
        }

        public FormOrderShipmentMessage(OrderCondition oc) : this()
        {
            this._orderCondition = oc;
            Orders orders = new Orders();
            try
            {
                this._dt = orders.GetShipmentMessgeByCondition(this._orderCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (this._dt != null)
            {
                if (this._dt.Rows.Count > 0)
                {
                    this.dg.DataSource = null;
                    this.dg.DataSource = this._dt;
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n订单项信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dg.DataSource = null;
                    base.Dispose();
                }
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dg.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dg.CurrentCell.RowIndex;
            new FormOrderInfo(this.dg.Rows[rowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void dgSumUp_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dg.Rows.Count - 1)
            {
                new FormOrderInfo(this.dg.Rows[e.RowIndex].Cells[2].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnCreateReportForms_Click(object sender, EventArgs e)
        {
            bool flag = false;
            for (int i = 0; i < this.dg.Rows.Count; i++)
            {
                if (Convert.ToBoolean(this.dg.Rows[i].Cells[0].Value))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                MessageBox.Show("没有选中要出发货通知的订单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Orders orders = new Orders();
                string text = null;
                try
                {
                    text = orders.GetShipMessageNumber();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                this.saveFileDialog1.Filter = "Excel文件|*.xls";
                this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
                this.saveFileDialog1.FileName = "发货通知单" + text + ".xls";
                string text2 = null;
                if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    text2 = this.saveFileDialog1.FileName;
                    this.saveFileDialog1.Dispose();
                    try
                    {
                        File.Copy(Application.StartupPath + "\\model\\ShipmentMessage.xls", text2, true);
                    }
                    catch (IOException ex3)
                    {
                        MessageBox.Show(ex3.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    try
                    {
                        ExcelOperationOrderShipmentMessageForm excelOperationOrderShipmentMessageForm = new ExcelOperationOrderShipmentMessageForm(Application.StartupPath);
                        excelOperationOrderShipmentMessageForm.MakeDataToExcel(text2, this._dt.Copy(), text, LoginInfo.OperatorName);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    this.saveFileDialog1.Dispose();
                }
            }
        }

        private void dgSumUp_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    if (!(bool)this.dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value && (bool)this.dg.Rows[e.RowIndex].Cells[1].Value)
                    {
                        if (MessageBox.Show("此订单已经生成发货通知单，是否继续？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    Orders orders = new Orders();
                    bool flag = false;
                    try
                    {
                        flag = orders.ShipMessage(this.dg.Rows[e.RowIndex].Cells[2].Value.ToString(), !(bool)this.dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    orders.Dispose();
                    if (flag)
                    {
                        if ((bool)this.dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)
                        {
                            this.dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                        }
                        else
                        {
                            this.dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                        }
                    }
                }
            }
        }

        private void FormOrderSumUp_Load(object sender, EventArgs e)
        {
            this.dg.AutoResizeColumns();
            if (!LoginInfo.Role[PowerName.Order_Delete])
            {
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
